import tkinter as tk
from tkinter import messagebox
import json
import os

USERS_FILE = "users.json"

# 初始化用户文件
def init_users_file():
    if not os.path.exists(USERS_FILE):
        with open(USERS_FILE, 'w') as f:
            json.dump({}, f)

# 读取用户数据
def load_users():
    with open(USERS_FILE, 'r') as f:
        return json.load(f)

# 保存用户数据
def save_users(users):
    with open(USERS_FILE, 'w') as f:
        json.dump(users, f)

# 登录窗口类
class LoginWindow:
    def __init__(self, root):
        self.root = root
        self.root.title("用户登录")
        self.root.geometry("300x250")
        self.create_widgets()

    def create_widgets(self):
        tk.Label(self.root, text="账号").pack(pady=5)
        self.username_entry = tk.Entry(self.root)
        self.username_entry.pack(pady=5)

        tk.Label(self.root, text="密码").pack(pady=5)
        self.password_entry = tk.Entry(self.root, show="*")
        self.password_entry.pack(pady=5)

        tk.Button(self.root, text="登录", command=self.login).pack(pady=10)
        tk.Button(self.root, text="注册", command=self.register).pack(pady=5)
        tk.Button(self.root, text="重置密码", command=self.reset_password).pack(pady=5)

    def login(self):
        username = self.username_entry.get()
        password = self.password_entry.get()
        users = load_users()
        if username in users and users[username] == password:
            messagebox.showinfo("成功", "登录成功！")
            self.root.destroy()
            from yolo_app import YOLODetectorApp
            main_root = tk.Tk()
            YOLODetectorApp(main_root)
            main_root.mainloop()
        else:
            messagebox.showerror("错误", "账号或密码错误！")

    def register(self):
        RegisterWindow()

    def reset_password(self):
        ResetPasswordWindow()

# 注册窗口
class RegisterWindow:
    def __init__(self):
        self.win = tk.Toplevel()
        self.win.title("注册")
        self.win.geometry("300x200")

        tk.Label(self.win, text="新账号").pack(pady=5)
        self.username_entry = tk.Entry(self.win)
        self.username_entry.pack(pady=5)

        tk.Label(self.win, text="新密码").pack(pady=5)
        self.password_entry = tk.Entry(self.win, show="*")
        self.password_entry.pack(pady=5)

        tk.Button(self.win, text="注册", command=self.register).pack(pady=10)

    def register(self):
        username = self.username_entry.get()
        password = self.password_entry.get()
        if not username or not password:
            messagebox.showerror("错误", "账号和密码不能为空！")
            return
        users = load_users()
        if username in users:
            messagebox.showerror("错误", "该账号已存在！")
        else:
            users[username] = password
            save_users(users)
            messagebox.showinfo("成功", "注册成功！")
            self.win.destroy()

# 重置密码窗口
class ResetPasswordWindow:
    def __init__(self):
        self.win = tk.Toplevel()
        self.win.title("重置密码")
        self.win.geometry("300x200")

        tk.Label(self.win, text="请输入账号").pack(pady=5)
        self.username_entry = tk.Entry(self.win)
        self.username_entry.pack(pady=5)

        tk.Button(self.win, text="重置密码", command=self.reset).pack(pady=10)

    def reset(self):
        username = self.username_entry.get()
        users = load_users()
        if username in users:
            users[username] = "000000"
            save_users(users)
            messagebox.showinfo("成功", "密码已重置为 000000")
            self.win.destroy()
        else:
            messagebox.showerror("错误", "账号不存在！")
